#include "stm32f10x.h"             
#include "delay.h"
#include "OLED.h"
#include "uart.h"
#include "esp8266.h"

#define wifi_ssid " "      //WIFI名称
#define wifi_pswd " " //WIFI密码
#define api_ipaadr "116.62.81.138" // 心知天气api的ip地址
#define api_port   80              // 端口号
#define my_key " " // API密钥
#define city   " "          // 城市拼音

                    
int main(void)
{  
    // 获取信息
    W_MSG msg;
    OLED_Init();
    UART1_Init(115200);
	
    if(ESP_OK != ESP8266_Init(115200))
    {
        OLED_ShowString(0,0,"ESP8266Init ERR",OLED_6X8);
    }else{
        OLED_ShowString(0,0,"ESP8266Init OK",OLED_6X8);
    }
    OLED_Update();
    if(ESP_OK != ESP_ConnectWIFI(wifi_ssid,wifi_pswd))
    {
        OLED_ShowString(0,8,"WIFIConeect ERR",OLED_6X8);
    }
    else{
        OLED_ShowString(0,8,"WIFIConeect OK",OLED_6X8);
    }
    OLED_Update();
    delay_ms(1000);
    OLED_Clear();
    
    while(1){

        if(ESP_ERR != ESP_TcpConnect(api_ipaadr,api_port))
        {       
            if(ESP_ERR != ESP_TcpGetWeather(my_key,city,&msg))
            {
                OLED_ShowString(30,4,msg.country,OLED_8X16);
                OLED_ShowChinese(70,4,msg.name);
                OLED_ShowChinese(30,20,"天气");
                OLED_ShowChinese(70,20,msg.text);
                OLED_ShowChinese(30,36,"温度");
                OLED_ShowNum(70,36,msg.temp,2,OLED_8X16);
                OLED_ShowChinese(86,36,"度");
                OLED_Update();
            }
        }

        delay_ms(1000); // 免费会员请求频率为20次/min，防止请求过频繁
        OLED_Clear();
    }
}


